package com.word.android.sdk;

import android.text.TextUtils;
import android.util.Log;

import com.word.android.sdk.ad.VideoRewardCallback;
import com.word.android.sdk.ad.WordVideoIAP;
import com.word.android.sdk.bridge.WordCocosBridge;
import com.word.android.sdk.ad.WordKwaiReward;
import com.word.android.sdk.ad.WordVideoManger;
import com.word.android.sdk.ad.WordMaxReward;
import com.word.android.sdk.utils.WordSPUtils;

import org.json.JSONObject;

public class WordAdSdk {

    public static void showBiddingAd(String ad_source) {
        showAd(new VideoRewardCallback() {
            @Override
            public void onAdOver() {
                adCallBack("ad_over_w",ad_source);
            }

            @Override
            public void onAdError() {
                WordCocosBridge.getInstance().XSSdkCallback("ad_error_w", "{}");
            }
        });
    }

    private static void adCallBack(String state,String ad_source) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ad_source",ad_source);
            WordCocosBridge.getInstance().XSSdkCallback(state, jsonObject.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void showAd(VideoRewardCallback callback) {
        WordVideoIAP service = null;
        boolean isMaxReady =  WordMaxReward.getInstance().isReady();
        boolean isKwaiReady =  WordKwaiReward.getInstance().isReady();
        if (isMaxReady){
            service= WordVideoManger.getReward1Instance();
            Log.e("WTMA","show max reward");
        }else if (isKwaiReady){
            service= WordVideoManger.getReward2Instance();
            Log.e("WTMA","show kwai reward");
        }

        if (service == null) {
            callback.onAdError();
            return;
        }
        showAd(callback, service);

        String country = WordSPUtils.getString(Constant.KEY_COUNTRY,"");
        if (!TextUtils.isEmpty(country)) {
            if (Constant.COUNTRY_ID.equals(country) || Constant.COUNTRY_BR.equals(country)) {
                //检查kwai是否需要load
                if (service instanceof WordMaxReward) {
                    WordKwaiReward.getInstance().loadRewardAd();
                }
            }
        }
    }

    public static void showAd(VideoRewardCallback callback, WordVideoIAP service) {
        try {
            service.showVideo(WordCocosBridge.mainGameActivity.get(), new VideoRewardCallback() {
                @Override
                public void onAdOver() {
                    if (callback != null) {
                        callback.onAdOver();
                    }
                }

                @Override
                public void onAdError() {
                    if (callback != null) {
                        callback.onAdError();
                    }
                }
            });
        } catch (Exception e) {
            try {
                callback.onAdError();
            } catch (Exception e1) {
            }
        }
    }


}
